package io.geekidea.boot.shop.service.base.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.shop.dto.GoodsDto;
import io.geekidea.boot.shop.entity.Goods;
import io.geekidea.boot.shop.mapper.GoodsMapper;
import io.geekidea.boot.shop.query.app.AppGoodsQuery;
import io.geekidea.boot.shop.query.web.GoodsQuery;
import io.geekidea.boot.shop.service.base.api.GoodsService;
import io.geekidea.boot.shop.vo.app.AppGoodsVo;
import io.geekidea.boot.shop.vo.web.GoodsVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *  服务实现类
 *
 * @author geekidea
 * @since 2024-10-27
 */
@Slf4j
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addGoods(GoodsDto dto) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(dto, goods);
        return save(goods);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateGoods(GoodsDto dto) {
        Integer id = dto.getId();
        Goods goods = getById(id);
        if (goods == null) {
            throw new BusinessException("不存在");
        }
        BeanUtils.copyProperties(dto, goods);
        return updateById(goods);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteGoods(Integer id) {
        return removeById(id);
    }

    @Override
    public GoodsVo getGoodsById(Integer id) {
        return goodsMapper.getGoodsById(id);
    }

    @Override
    public Paging<GoodsVo> getGoodsPage(GoodsQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<GoodsVo> list = goodsMapper.getGoodsPage(query);
        Paging<GoodsVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppGoodsVo getAppGoodsById(Integer id) {
        return goodsMapper.getAppGoodsById(id);
    }

    @Override
    public Paging<AppGoodsVo> getAppGoodsPage(AppGoodsQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppGoodsVo> list = goodsMapper.getAppGoodsPage(query);
        Paging<AppGoodsVo> paging = new Paging<>(list);
        return paging;
    }

}
